package com.chuanyun.wechatcheckin.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门字段反序列化器
 * 处理企业微信API返回的department字段可能是单个整数或整数数组的情况
 */
public class DepartmentDeserializer extends JsonDeserializer<List<Integer>> {
    
    @Override
    public List<Integer> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        List<Integer> departments = new ArrayList<>();
        
        JsonToken token = parser.getCurrentToken();
        
        if (token == JsonToken.VALUE_NUMBER_INT) {
            // 如果是单个整数，添加到列表中
            departments.add(parser.getIntValue());
        } else if (token == JsonToken.START_ARRAY) {
            // 如果是数组，逐个读取
            while (parser.nextToken() != JsonToken.END_ARRAY) {
                if (parser.getCurrentToken() == JsonToken.VALUE_NUMBER_INT) {
                    departments.add(parser.getIntValue());
                }
            }
        }
        
        return departments;
    }
}